package com.ruoyi.base.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.base.domain.ComponentClassification;
import com.ruoyi.base.service.IComponentClassificationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 构件分类Controller
 *
 * @author ruoyi
 * @date 2022-08-16
 */
@RestController
@RequestMapping("/base/classification")
public class ComponentClassificationController extends BaseController {
    @Autowired
    private IComponentClassificationService componentClassificationService;

    /**
     * 查询构件分类列表
     */
    @PreAuthorize("@ss.hasPermi('base:classification:list')")
    @GetMapping("/list")
    public TableDataInfo list(ComponentClassification componentClassification) {
        startPage();
        List<ComponentClassification> list = componentClassificationService.selectComponentClassificationList(componentClassification);
        return getDataTable(list);
    }

    /**
     * 导出构件分类列表
     */
    @PreAuthorize("@ss.hasPermi('base:classification:export')")
    @Log(title = "构件分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ComponentClassification componentClassification) {
        List<ComponentClassification> list = componentClassificationService.selectComponentClassificationList(componentClassification);
        ExcelUtil<ComponentClassification> util = new ExcelUtil<ComponentClassification>(ComponentClassification.class);
        util.exportExcel(response, list, "构件分类数据");
    }

    /**
     * 获取构件分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('base:classification:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(componentClassificationService.selectComponentClassificationById(id));
    }

    /**
     * 新增构件分类
     */
    @PreAuthorize("@ss.hasPermi('base:classification:add')")
    @Log(title = "构件分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ComponentClassification componentClassification) {
        if (StringUtils.isBlank(componentClassification.getCategoryCode())) {
            AjaxResult.error("类别代码不能为空!");
        }
        return toAjax(componentClassificationService.insertComponentClassification(componentClassification));
    }

    /**
     * 修改构件分类
     */
    @PreAuthorize("@ss.hasPermi('base:classification:edit')")
    @Log(title = "构件分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ComponentClassification componentClassification) {
        return toAjax(componentClassificationService.updateComponentClassification(componentClassification));
    }

    /**
     * 删除构件分类
     */
    @PreAuthorize("@ss.hasPermi('base:classification:remove')")
    @Log(title = "构件分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(componentClassificationService.deleteComponentClassificationByIds(ids));
    }

    /**
     * 查询构件分类列表
     */
    @PreAuthorize("@ss.hasPermi('base:classification:list')")
    @GetMapping("/getComponenTypeList")
    public TableDataInfo getComponenTypeList(ComponentClassification componentClassification) {
        List<ComponentClassification> list = componentClassificationService.getComponenTypeList();
        return getDataTable(list);
    }
}
